package com.neuedu.hisboot.controller;

import com.neuedu.hisboot.dto.Result;
import com.neuedu.hisboot.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  系统维护管理控制器
 * </p>
 *
 * @author jaun
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private ISystemService systemService;

    /**
     * 系统登录状态检查
     */
    @GetMapping("/status")
    public Result<String> getSystemStatus() {
        return Result.success("系统运行正常");
    }

    /**
     * 数据备份
     */
    @PostMapping("/backup")
    public Result<String> backupData() {
        try {
            boolean success = systemService.backupData();
            if (success) {
                return Result.success("数据备份成功");
            } else {
                return Result.error(500, "数据备份失败");
            }
        } catch (Exception e) {
            return Result.error(500, "数据备份时发生错误: " + e.getMessage());
        }
    }

    /**
     * 数据恢复
     */
    @PostMapping("/restore")
    public Result<String> restoreData(@RequestParam("backupFile") String backupFile) {
        try {
            boolean success = systemService.restoreData(backupFile);
            if (success) {
                return Result.success("数据恢复成功");
            } else {
                return Result.error(500, "数据恢复失败");
            }
        } catch (Exception e) {
            return Result.error(500, "数据恢复时发生错误: " + e.getMessage());
        }
    }

    /**
     * 获取系统信息
     */
    @GetMapping("/info")
    public Result<Object> getSystemInfo() {
        try {
            Object systemInfo = systemService.getSystemInfo();
            return Result.success(systemInfo);
        } catch (Exception e) {
            return Result.error(500, "获取系统信息时发生错误: " + e.getMessage());
        }
    }

    /**
     * 清理临时文件
     */
    @PostMapping("/cleanup")
    public Result<String> cleanupTempFiles() {
        try {
            boolean success = systemService.cleanupTempFiles();
            if (success) {
                return Result.success("临时文件清理成功");
            } else {
                return Result.error(500, "临时文件清理失败");
            }
        } catch (Exception e) {
            return Result.error(500, "清理临时文件时发生错误: " + e.getMessage());
        }
    }

    /**
     * 系统维护模式切换
     */
    @PostMapping("/maintenance")
    public Result<String> toggleMaintenanceMode(@RequestParam("enabled") Boolean enabled) {
        try {
            boolean success = systemService.toggleMaintenanceMode(enabled);
            if (success) {
                String message = enabled ? "系统已进入维护模式" : "系统已退出维护模式";
                return Result.success(message);
            } else {
                return Result.error(500, "维护模式切换失败");
            }
        } catch (Exception e) {
            return Result.error(500, "维护模式切换时发生错误: " + e.getMessage());
        }
    }
}

